﻿using System;
using System.Collections.Generic;

namespace SpatialHashLib.Topology.Indexing
{
    struct CellExtent
    {
        public readonly CellCoord Min;
        public readonly CellCoord Max;

        public CellExtent(CellCoord p, CellCoord q)
        {
            Min = new CellCoord((short)Math.Min(p.Row, q.Row), (short)Math.Min(p.Col, q.Col));
            Max = new CellCoord((short)Math.Max(p.Row, q.Row), (short)Math.Max(p.Col, q.Col));
        }

        public CellExtent(Extent extent, double cellSize)
        {
            Min = new CellCoord(
                (short)Math.Floor(extent.Min.Y / (double)cellSize),
                (short)Math.Floor(extent.Min.X / (double)cellSize)
                );

            Max = new CellCoord(
                (short)Math.Ceiling(extent.Max.Y / (double)cellSize),
                (short)Math.Ceiling(extent.Max.X / (double)cellSize)
                );
        }

        public IEnumerable<CellCoord> CellCoords
        {
            get
            {
                for (int row = Min.Row; row < Max.Row; row++)
                    for (int col = Min.Col; col < Max.Col; col++)
                        yield return new CellCoord(row, col);
            }
        }
    }
}
